﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using KL.Store.Data;
using KL.Store.Web.Models;
using KL.Store.Core;
using KL.Store.Core.Domain;

namespace KL.Store.Web.Controllers
{
    public class HomeController : KL.Store.Core.Web.BaseController
    {
        public ActionResult Index()
        {
            ViewBag.Controller = "home_ctrl";

            var homeModel = new HomeModel
            {
                BannerList = dbBanner.GetList().Take(Parameters.HomePageSliderMaxImageCount).ToList()
            };

            var cm = dbCategory.GetMeta(Parameters.HomePageCategoryId);
            if (cm != null)
            {
                ViewBag.Title = cm.CategoryName ?? "Ana Sayfa";
                ViewBag.Keywords = cm.Keywords ?? "";
                ViewBag.Description = cm.Description ?? "";
                ViewBag.ColumnCount = cm.ColumnCount;
                ViewBag.ListImageType = cm.ListImageType;
            }
            else
            {
                ViewBag.Title = "Ana Sayfa";
            }

            #region Commented

            //   Logger.Write("test Controller", "Trace");
            //int i = Convert.ToInt32("sdasd");

            //var cacheManager = CacheFactory.GetCacheManager("Cache Manager");

            //string menustr = (string)cacheManager.GetData("CategoryMenu");
            //if (menustr == null)
            //{
            //    try
            //    {
            //        menustr = dbCategoryMenu.Get(2);

            //        cacheManager.Add("CategoryMenu", menustr, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
            //        cacheManager.Add("CategoryMenu_Long", menustr, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddDays(1)));
            //    }
            //    catch
            //    {
            //        cacheManager.Add("CategoryMenu", cacheManager.GetData("CategoryMenu_Long").ToString(), CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
            //        menustr = cacheManager.GetData("CategoryMenu").ToString();
            //    }
            //}
            //ViewBag.Menu = menustr; 

            #endregion

            #region HomePage Groups

            var cacheManager = CacheFactory.GetCacheManager("Cache Manager");

            IList<ProductGroup> groups = (List<ProductGroup>)cacheManager.GetData("GroupsMenu");
            if (groups == null)
            {
                try
                {
                    groups = dbProduct.GetGroupList(Parameters.HomePageCategoryId);

                    cacheManager.Add("GroupsMenu", groups, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
                    cacheManager.Add("GroupsMenu_Long", groups, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddDays(1)));
                }
                catch
                {
                    cacheManager.Add("GroupsMenu", cacheManager.GetData("GroupsMenu_Long").ToString(), CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
                    groups = (List<ProductGroup>)cacheManager.GetData("GroupsMenu");
                }
            }

            if (groups != null)
            {
                homeModel.HomePageGroups = groups.OrderBy(o => o.GroupCode).ToList();
            }

            #endregion

            #region HomePage Tab Groups

            IList<ProductGroup> tabgroups = (List<ProductGroup>)cacheManager.GetData("TabGroupsMenu");
            if (tabgroups == null)
            {
                try
                {
                    tabgroups = dbProduct.GetGroupList(Parameters.HomePageTabCategoryId);

                    cacheManager.Add("TabGroupsMenu", tabgroups, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
                    cacheManager.Add("TabGroupsMenu_Long", tabgroups, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddDays(1)));
                }
                catch
                {
                    cacheManager.Add("TabGroupsMenu", cacheManager.GetData("TabGroupsMenu_Long").ToString(), CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
                    tabgroups = (List<ProductGroup>)cacheManager.GetData("TabGroupsMenu");
                }
            }

            if (tabgroups != null)
            {
                homeModel.HomePageTabGroups = tabgroups.OrderBy(o => o.GroupCode).ToList();
            }

            #endregion


            homeModel.HomePageTabGroup = dbCategory.Get(Parameters.HomePageTabCategoryId);


            eStoreUser.RedirectUrl = "/";
            return View(homeModel);
        }

        public ActionResult Robots()
        {
            Response.ContentType = "text/plain";
            return View();
        }

        public ActionResult Image(string url)
        {
            var ext = Path.GetExtension(url);
            var typ = "";
            switch (ext.ToLower())
            {
                case "jpg": typ = "image/jpeg"; break;
                case "png": typ = "image/png"; break;
                case "gif": typ = "image/gif"; break;
                case "bmp": typ = "image/bitmap"; break;
                default: typ = "image/jpeg"; break;

            }
            return new FileStreamResult(new FileStream(url, FileMode.Open), typ);
        }

        public ActionResult ReadyLightbox()
        {
            return View();
        }

        public ActionResult ManagerScripts()
        {
            ViewBag.IsManager = eStoreUser.IsManager;
            ViewBag.IsSeoManager = eStoreUser.IsSeoManager;
            ViewBag.IsReportManager = eStoreUser.IsReportManager;
            ViewBag.IsAllowedBuyPrice = eStoreUser.IsAllowedBuyPrice;
            return View();
        }
    }
}
